﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using RealtorsPortal.Data_Access_Layer;
using RealtorsPortal.Entities;
using System.Data;
using RealtorsPortal.Business_Logic_Layer;
using System.Collections;

namespace RealtorsPortal.AdminCP
{
    /// <summary>
    /// Summary description for Test
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    [ScriptService]
    public class Test : System.Web.Services.WebService
    {

        List<Country> list = new List<Country>();

        public Test() {
            LoadCountries();
        }

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }


        private void LoadCountries() {
            CountriesDAL countryDAL = new CountriesDAL();
            DataTable dt = new DataTable();
            dt = countryDAL.getCountriesData();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(new Country{ CountryId = Convert.ToInt16(row["CountryId"]), CountryName = row["CountryName"].ToString()});
            }  
        }

        [WebMethod]
        public List<Country> GetAllCountries()
        {
            return list;
        } 

        [WebMethod]
        public string GetRegionForCityTab(int countryID)
        {
            Region regionObj = new Region();
            regionObj.CountryId = countryID;
            DataTable dt = new DataTable();
            RegionDAL regDAL = new RegionDAL();
            dt = regDAL.GetRegionsByCountryId(regionObj);
            List<Region> list = new List<Region>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(new Region
                {
                    RegionId = Convert.ToInt16(row["RegionId"]),
                    RegionName = row["RegionName"].ToString(),
                    CountryId = Convert.ToInt16(row["CountryId"])
                });
            }          
            return Utilities.ConvertToJson(list);
        }

        [WebMethod]
        public string GetCityForDistrictTab(int regionID)
        {
            City cityObj = new City();
            cityObj.RegionId = regionID;
            DataTable dt = new DataTable();
            CitiesDAL cityDAL = new CitiesDAL();
            dt = cityDAL.GetCitiesByRegionId(cityObj);
            List<City> list = new List<City>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(new City
                {
                    CityId = Convert.ToInt16(row["CityId"]),
                    CityName = row["CityName"].ToString(),
                    RegionId = Convert.ToInt16(row["RegionId"])
                });
            }
            return Utilities.ConvertToJson(list);
        }

        public void InsertRegionUsingJquery(string regionName, string countryId) {
            Region regObj = new Region();
            regObj.RegionName = regionName;
            regObj.CountryId = int.Parse(countryId);
            RegionDAL regDAL = new RegionDAL();
            regDAL.InsertRegions(regObj);
        }
    }
}
